package com.atwisdom.star.core.dsl.modelClass.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DictionaryHier {
    private String type = "dictionary";
    private String className;
    private Long containerId;
    private String containerModel;

    private List<DictionaryHierContent> content = new ArrayList<>();

    public List<DictionaryHierContent> getContent() {
        return content;
    }

    public void setContent(List<DictionaryHierContent> content) {
        this.content = content;
    }



    public DictionaryHier(JSONObject hierProperties) {
        if(hierProperties==null || hierProperties.size() < 0){
            return;
        }
        this.type = hierProperties.getString("type");
//        this.className = hierProperties.getString("className");
//        this.containerModel = hierProperties.getString("containerModel");
        JSONArray tempContent = hierProperties.getJSONArray("content");
        for (int i = 0; i < tempContent.size(); i++) {
            JSONObject contentJson = tempContent.getJSONObject(i);
            JSONArray properties = contentJson.getJSONArray("properties");
            JSONObject jsonObject = this.fetchValues(properties);
            DictionaryHierContent hierContent = JSONObject.parseObject(JSON.toJSONString(contentJson), DictionaryHierContent.class);
            hierContent.setValues(jsonObject);
            hierContent.setType(type);
            content.add(hierContent);
        }
    }
    public DictionaryHier (JSONObject hierProperties, JSONObject hier) {
        this(hierProperties);
        if(hierProperties==null || hierProperties.size() <1){
            return;
        }

        if(hier==null){
            return;
        }

        DictionaryHier dictionaryHier = JSONObject.parseObject(JSON.toJSONString(hier), DictionaryHier.class);
        this.getContent().forEach(c -> {
            JSONObject initValues = c.getValues();
            DictionaryHierContent dictHierContent = dictionaryHier.getContent().stream().filter(item -> item.getId().equalsIgnoreCase(c.getId())).findFirst().orElse(null);
            if(dictHierContent!=null){
                JSONObject updateValues = dictHierContent.getValues();
                JSONObject values = JsonUtil.merge(initValues, updateValues);
                c.setValues(values);
                c.setLink(dictHierContent.getLink());
            }
        });
    }


    /**
     * 获取默认values
     *
     * @return 返回结果
     */
    public JSONObject fetchValues(JSONArray properties) {
        JSONObject values = new JSONObject();
        for (Object item : properties) {
            JSONObject itemJson = JsonUtil.toJSONObject(item);
            String name = itemJson.getString("name");
            if(StringUtils.isNotBlank(name)){
                values.put(itemJson.getString("name"), itemJson.get("defaultValue"));
            }
        }
        return values;
    }

    /**
     * 将当前实例转换成JSONObject
     * @return 返回结果
     */
    public JSONObject fetchJsonObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", this.type);
        JSONArray contentAry = JSONArray.parseArray(JSONArray.toJSONString(this.content));
        jsonObject.put("content", contentAry);
        return jsonObject;
    }

    public static JSONObject fetchUpdateHier(JSONObject hierProperties, JSONObject updateHier) {
        if(hierProperties==null || hierProperties.size() <1){
            return new JSONObject();
        }
        DictionaryHier initHier = new DictionaryHier(hierProperties);
        if(updateHier==null || updateHier.size() == 0){
            return initHier.fetchJsonObject();
        }

        DictionaryHier updateDictionaryHier = JSONObject.parseObject(JSON.toJSONString(updateHier), DictionaryHier.class);
        List<DictionaryHierContent> hierContents = new ArrayList<>();
        initHier.getContent().forEach(c -> {
            JSONObject link = c.getLink();
            String modelLinkClassName = link.getString("className");
            JSONObject initValues = c.getValues();
            DictionaryHierContent dictHierContent = updateDictionaryHier.getContent().stream().filter(item -> item.getId().equalsIgnoreCase(c.getId())).findFirst().orElse(null);
            if(dictHierContent!=null){
                JSONObject contentLink = dictHierContent.getLink();
                String insLinkClassName = contentLink.getString("className");
                JSONObject updateValues = dictHierContent.getValues();
                JSONObject values = JsonUtil.merge(initValues, updateValues);
                dictHierContent.setValues(values);
                if(!modelLinkClassName.equalsIgnoreCase(insLinkClassName)){
                    dictHierContent.setLink(link);
                }
                hierContents.add(dictHierContent);
            }else{
                hierContents.add(c);
            }
        });
        updateDictionaryHier.setContent(hierContents);
        return updateDictionaryHier.fetchJsonObject();
    }
}
